package de.projectgoodmorning.alarm.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import de.projectgoodmorning.alarm.R;
import de.projectgoodmorning.alarm.activities.views.NumberPicker;
import de.projectgoodmorning.alarm.dialogs.ColorPickerDialog.OnColorPicked;
import de.projectgoodmorning.alarm.model.gateway.GatewayAlarm;

/**
 * Der CommandDialog ist ein Dialog der das Erstellen eines
 * GatewayAlarms ermoeglicht.
 * 
 * @see GatewayAlarm
 */
public class CommandDialog extends Dialog implements OnClickListener {
	private static final String TAG = CommandDialog.class.getName();

	private Button colorButton, add, cancel;
	private NumberPicker minutePicker, secondsPicker;
	private RadioButton beforeRadioButton, afterRadioButton;

	private CommandListener listener;
	private GatewayAlarm alarm;

	/**
	 * erstellt einen neuen CommandDialog.
	 * 
	 * @param context		erwartet den Context
	 * @param listener		erwartet einen CommandListener, welcher
	 * 						ausgefuehrt wird, wenn "ok" gewaehlt wird.
	 */
	public CommandDialog( Context context, CommandListener listener ) {
		super( context );
		this.listener = listener;
		
		setContentView( R.layout.add_command );
		setTitle( context.getResources().getString( R.string.addCommand ) );

		add = ( Button ) findViewById( R.id.confirmDialogButton );
		cancel = ( Button ) findViewById( R.id.cancelDialogButton );

		add.setOnClickListener( this );
		cancel.setOnClickListener( this );

		colorButton = ( Button ) findViewById( R.id.colorButton );
		colorButton.setOnClickListener( this );
		colorButton.setBackgroundColor( Color.YELLOW );
		
		minutePicker = ( NumberPicker ) findViewById( R.id.numberPickerMinutes );
		secondsPicker = ( NumberPicker ) findViewById( R.id.numberPickerSeconds );
		beforeRadioButton = ( RadioButton ) findViewById( R.id.beforeRadioButton );
		afterRadioButton = ( RadioButton ) findViewById( R.id.afterRadioButton );

		minutePicker.setMinimum( 0 );
		minutePicker.setMaximum( 60 );
		
		secondsPicker.setMinimum( 0 );
		secondsPicker.setMaximum( 60 );
		
		beforeRadioButton.setChecked( true );
		
		alarm = new GatewayAlarm( 0, Color.YELLOW );
	}

	@Override
	public void onClick( View v ) {
		if( v == add ) {
			if( listener != null ) {
				int addSecs = ( minutePicker.getValue() * 60 ) + secondsPicker.getValue(); 
				if( beforeRadioButton.isChecked() ) {
					addSecs = -addSecs;
				}

				alarm.seconds = addSecs;
				listener.command( alarm );
			}
			dismiss();
		}

		if( v == cancel ) {
			dismiss();
		}
		
		if( v == colorButton ) {
			new ColorPickerDialog( getContext(), alarm.color,
					new OnColorPicked() {
						@Override
						public void onCancel( ColorPickerDialog dialog ) {
						}

						@Override
						public void onOk( ColorPickerDialog dialog, int newColor ) {
							alarm.color = newColor;
							Log.d( TAG, "New color picked: " + newColor );
							
							setColorButton( alarm.color );
						}
					} ).show();
		}
	}
	
	/**
	 * belegt die Werte des Dialogs durch einen uebergebene Alarm
	 * @param alarm		erwartet einen GatewayAlarm
	 */
	public void setAlarm( GatewayAlarm alarm ) {
		this.alarm = alarm;
		setColor( alarm.color );
		setTime( alarm.seconds );
	}
	
	private void setColor( int color ) {
		setColorButton( color );
	}
	
	private void setColorButton( int color ) {
		colorButton.setBackgroundColor( color );
		if( color == Color.BLACK ) {
			colorButton.setTextColor( Color.WHITE );
		} else {
			colorButton.setTextColor( Color.BLACK );
		}
	}
	
	private void setTime( int seconds ) {
		if( seconds <= 0 ) {
			beforeRadioButton.setChecked( true );
		} else {
			afterRadioButton.setChecked( true );
		}
		
		alarm.seconds = seconds;
		seconds = Math.abs( seconds );
		
		int mins = seconds / 60;
		int secs = seconds % 60;
		minutePicker.setValue( mins );
		secondsPicker.setValue( secs );
		
	}
}
